# -*- coding: utf-8 -*-

import os
import traceback
import commonlib
import datetime
import time
import re
from MongoDbLog import MongoDbLog
from MongoModel import MongoModel

LOG = MongoDbLog('xoso_ketqua_net', 'debug')
MAX_PROCESSED = 60

class KetQua(MongoModel):
    
    mienBac = {'XSTD': 'mb'}
    mienTrung = {'XSBDH': 'bdi','XSDLK': 'dlc','XSGL': 'gl','XSKH': 'kh','XSKT': 'kt','XSNT': 'nt','XSPY': 'py','XSQB': 'qb','XSQNM': 'qna','XSQNI': 'qng','XSQT': 'qt','XSTTH': 'tth','XSDNG': 'dna','XSDNO': 'dno'}
    mienNam = {'XSAG': 'ag','XSBD': 'bdu','XSBP': 'bp','XSBTH': 'bth','XSBL': 'bl','XSBTR': 'btr','XSCM': 'cm','XSCT': 'ct','XSHG': 'hg','XSHCM': 'hcm','XSKG': 'kg','XSLA': 'la','XSST': 'st','XSTG': 'tg','XSTV': 'tv','XSTN': 'tn','XSVL': 'vl','XSVT': 'vt','XSDL': 'dlt','XSDN': 'dni','XSDT': 'dt'}
    
    def __init__(self, host, port):
        MongoModel.__init__(self, host, port)
    
    def processMien(self, giaiId, ma, mien):
        url = 'http://ketqua.net/xo-so/so-ket-qua.php'
        post = {'cmdView': 'Xem kết quả', 'end_date': datetime.datetime.now().strftime("%d/%m/%Y"), 'txtNumber': '30'}
        post['slcTinh'] = giaiId
        currentProcessed = 0
        try:
            while currentProcessed <= MAX_PROCESSED:
                tree = commonlib.getXMLTree(url, post, headers={'Content-Type': 'application/x-www-form-urlencoded'})
                wrapper  = {'Đặc Biệt': 'DB', 'Giải Nhất': 'G1', 'Giải Nhì': 'G2', 'Giải Ba': 'G3', 'Giải Tư': 'G4', 'Giải Năm': 'G5', 'Giải Sáu': 'G6', 'Giải Bảy': 'G7', 'Giải Tám': 'G8'}
                lDateObj = ''
                for item in tree.xpath("//div[@class='span-10 space']"):
                    currentProcessed += 1
                    if (currentProcessed > MAX_PROCESSED): break
                    xsNameNDate = commonlib.getElementText(item.xpath(".//th//h2"), descendant=1)
                    xsName = commonlib.extractWithRegEx(r'(.+)Mở thưởng', xsNameNDate, 1)
                    xsDate = commonlib.extractWithRegEx(r'ngày (\d+/\d+/\d{4})', xsNameNDate, 1)
                    print xsName
                    print xsDate
                    xsDateObj = datetime.datetime.strptime(xsDate, "%d/%m/%Y")
                    lDateObj = xsDateObj
                    result = {'_id': str(hash(ma + xsDate)),
                        'name': xsName,
                        'ma': ma,
                        'date': xsDate,
                        'dateobj': xsDateObj,
                        'mien': mien,
                        'lastupdate': datetime.datetime.now(),
                        'timestamp': time.time(),
                    }
                    for i, v in wrapper .items():
                        nNode = item.xpath(".//h3[contains(., '{0}')]/..".format(i).decode('utf-8'))
                        data = ''
                        if len(nNode) > 0:
                            if nNode[0].get('rowspan', '') == '2':
                                for ij in nNode[0].xpath("./following-sibling::*"):
                                    data += "{0}-".format(commonlib.getElementText(ij))
                                for ij in nNode[0].xpath("../following-sibling::*[1]/td"):
                                    data += "{0}-".format(commonlib.getElementText(ij))
                            else:
                                for ij in nNode[0].xpath("./following-sibling::*"):
                                    data += "{0}-".format(commonlib.getElementText(ij))
                            data = data[:-1]
                        if v == 'DB' and re.search(r'-', data):
                            dataArr = data.split('-')
                            result['DB6so'] = dataArr[1]
                            data = dataArr[0]
                        result[v] = data
                    db = self.connection['xoso']
                    collection = db[ma]         # NOTE: luu giong database cu~
                    collection.save(result)
                if lDateObj == '': break
                lDateObj += datetime.timedelta(days=-1)
                post['end_date'] = lDateObj.strftime("%d/%m/%Y")
        except:
            LOG.error(traceback.format_exc())
            
    def process(self):
        for i, v in self.mienBac.items():
            self.processMien(v, i, "Miền Bắc")
        for i, v in self.mienTrung.items():
            self.processMien(v, i, "Miền Trung")
        for i, v in self.mienNam.items():
            self.processMien(v, i, "Miền Nam")
            
if __name__ == '__main__':
    LOG.info('start crawler xoso {0} ngay'.format(MAX_PROCESSED))
    kq = KetQua('beta.mana.vn', 27017)
    kq.process()
    LOG.info('finished crawler xoso {0} ngay'.format(MAX_PROCESSED))
    os._exit(1)
    